package com.sec.android.app.myfiles.widget.tablet;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.activity.AbsMyFilesActivity;
import com.sec.android.app.myfiles.info.AppConstants;
import com.sec.android.app.myfiles.info.AppFeatures;
import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.navigation.NavigationInfo;
import com.sec.android.app.myfiles.navigation.NavigationManager;
import com.sec.android.app.myfiles.util.DrawableMgr;
import com.sec.android.app.myfiles.util.UiUtils;

import java.util.ArrayList;

/**
 * Created by jaebae on 2016-01-06.
 */
public class ColumnListHeaderView extends LinearLayout implements View.OnClickListener {
    private ImageView mSizeResizeView;

    private ArrayList<ColumnHeaderItemView> mColViewList;
    private ArrayList<ImageView> mResizeBarList;
    private boolean mSortEnabled;
    private NavigationInfo mNavigationInfo;

    private ImageView mCurResizingBar;
    private int mCurResizingViewIndex = -1;
    private int mCurResizingViewWidth;

    private ColumnEventListener mColumnEventListener;

    public ColumnListHeaderView(Context context) {
        super(context);
        init();
    }

    public ColumnListHeaderView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ColumnListHeaderView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        Context context = getContext();
        inflate(context, R.layout.column_list_header, this);

        ColumnHeaderItemView name = (ColumnHeaderItemView) findViewById(R.id.name_column);
        ColumnHeaderItemView date = (ColumnHeaderItemView) findViewById(R.id.date_column);
        ColumnHeaderItemView type = (ColumnHeaderItemView) findViewById(R.id.type_column);
        ColumnHeaderItemView size = (ColumnHeaderItemView) findViewById(R.id.size_column);

        ImageView nameResizeView = (ImageView) findViewById(R.id.name_divider);
        ImageView dateResizeView = (ImageView) findViewById(R.id.date_divider);
        ImageView typeResizeView = (ImageView) findViewById(R.id.type_divider);
        mSizeResizeView = (ImageView) findViewById(R.id.size_divider);

        UiUtils.setOnHoverListener(context, nameResizeView);
        UiUtils.setOnHoverListener(context, dateResizeView);
        UiUtils.setOnHoverListener(context, typeResizeView);
        UiUtils.setOnHoverListener(context, mSizeResizeView);

        mResizeBarList = new ArrayList<>();
        mColViewList = new ArrayList<>();

        setColumnView(context, name, nameResizeView, R.string.name, AppConstants.SortByType.NAME);
        setColumnView(context, date, dateResizeView, R.string.time, AppConstants.SortByType.TIME);
        setColumnView(context, type, typeResizeView, R.string.type, AppConstants.SortByType.TYPE);
        setColumnView(context, size, mSizeResizeView, R.string.size, AppConstants.SortByType.SIZE);

        setSortEnabled(context);
        updateSortIcon();
    }

    private void setSortEnabled(Context context) {
        mNavigationInfo = NavigationManager.getInstance(((AbsMyFilesActivity) context).getProcessId()).getCurInfo();
        if (mNavigationInfo != null) {
            FileRecord curRecord = mNavigationInfo.getCurRecord();

            if (curRecord != null) {
                mSortEnabled = (curRecord.getStorageType() != FileRecord.StorageType.Recent);
            }
        }
    }

    private void setColumnView(Context context, ColumnHeaderItemView colView, ImageView resizeView, int resTextId, int sortByType) {
        colView.setOnClickListener(this);
        addViewList(colView, resizeView);
        colView.setText(context.getResources().getString(resTextId));
        colView.setSortType(sortByType);
    }

    private void addViewList(ColumnHeaderItemView colView, ImageView resizeView) {
        mColViewList.add(colView);
        mResizeBarList.add(resizeView);
    }

    public void setColumnSizeChangedListener(ColumnEventListener l) {
        mColumnEventListener = l;
    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        boolean bRet = false;

        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                ImageView view = getPressedResizeBar(ev);
                if (view != null) {
                    requestDisallowInterceptTouchEvent(true);
                    setResizeBarPressed(view);
                    bRet = true;
                }
                break;

            case MotionEvent.ACTION_MOVE:
                setColumnArea(ev);
                bRet = true;
                break;

            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_POINTER_UP:
            case MotionEvent.ACTION_UP:
                if (mCurResizingViewIndex >= 0) {
                    if (mColumnEventListener != null) {
                        mColumnEventListener.onColumnSizeChangedFinished(mCurResizingViewIndex, mCurResizingViewWidth);
                    }

                    setResizeBarPressed(null);
                    requestDisallowInterceptTouchEvent(false);
                    bRet = true;
                }
                break;
            default:
                break;
        }

        if (!bRet) {
            bRet = super.dispatchTouchEvent(ev);
        }
        return bRet;
    }

    private ImageView getCurResizeBarView(ImageView view) {
        ImageView ret;

        if (view != null) {
            mCurResizingBar = view;
            ret = mCurResizingBar;
        } else {
            ret = mCurResizingBar;
            mCurResizingBar = null;
        }

        if (mCurResizingBar == null) {
            mCurResizingViewIndex = -1;
        } else {
            for (int i = 0; i < mResizeBarList.size(); i++) {
                if (mResizeBarList.get(i) == mCurResizingBar) {
                    mCurResizingViewIndex = i;
                    break;
                }
            }
        }

        return ret;
    }

    private void setResizeBarPressed(ImageView view) {
        ImageView curView = getCurResizeBarView(view);

        if (curView != null) {
            mCurResizingBar = view;
            ViewGroup.LayoutParams params = curView.getLayoutParams();

            if (view != null) {
                params.width = (int) getResources().getDimension(R.dimen.list_column_header_divider_pressed_width);
            } else {
                params.width = (int) (getResources().getDimension(R.dimen.list_column_header_divider_width) + 0.5);
            }

            int resId = R.color.color_primary_dark;

            if (view == null) {
                if (curView != mSizeResizeView) {
                    resId = R.drawable.index_tab_divider;
                } else {
                    resId = NO_ID;
                }
            }
            curView.setImageDrawable(DrawableMgr.getDrawable(getContext(), resId));

            curView.setLayoutParams(params);
            boolean isPressed = view != null;
            Log.d(this, "SetPressed = " + isPressed);
            curView.setPressed(isPressed);
            if (AppFeatures.isKnoxDesktopMode()) {
                UiUtils.setMousePointerIcon(getContext(), this, isPressed);
            }
        }
    }

    private ImageView getPressedResizeBar(MotionEvent ev) {
        ImageView ret = null;
        for (ImageView view : mResizeBarList) {
            if (isPressed(ev, view)) {
                ret = view;
                break;
            }
        }
        return ret;
    }

    private void updateSortIcon() {
        for (ColumnHeaderItemView headerItemView : mColViewList) {
            headerItemView.refreshSortCursor(mSortEnabled, mNavigationInfo);
        }
    }

    private boolean isPressed(MotionEvent ev, View view) {
        boolean bRet = false;
        int touchArea = (int) getResources().getDimension(R.dimen.split_view_divider_width);

        int LayoutPos = view.getLeft();

        float touchPos = ev.getX();

        if ((Math.abs(LayoutPos - touchPos) < touchArea)) {
            bRet = true;
        }

        return bRet;

    }

    private void setColumnArea(MotionEvent ev) {
        if (mCurResizingViewIndex >= 0) {
            ColumnHeaderItemView view = mColViewList.get(mCurResizingViewIndex);

            int nWidth;

            if (UiUtils.isInRTLMode(getContext())) {
                nWidth = view.getRight() - (int) ev.getX();
            } else {
                nWidth = (int) ev.getX() - view.getLeft();
            }

            mCurResizingViewWidth = view.setWidth(nWidth);
            if (mColumnEventListener != null) {
                mColumnEventListener.onColumnSizeChanged(mCurResizingViewIndex, mCurResizingViewWidth);
            }
        }
    }

    @Override
    public void onClick(View v) {
        Log.d(this, "is Clicked");
        if (mSortEnabled) {
            if (v instanceof ColumnHeaderItemView) {
                ColumnHeaderItemView view = (ColumnHeaderItemView) v;
                if (mColumnEventListener != null) {
                    mColumnEventListener.onSortByChanged(view.getSortType());
                }
            }

            updateSortIcon();
        }
    }

    public void setColumnHeaderText(int nColIndex, String text) {
        mColViewList.get(nColIndex).setText(text);
    }

    public int setColumnWidth(int columnIndex, int nWidth) {
        ColumnHeaderItemView view = mColViewList.get(columnIndex);
        nWidth = view.setWidth(nWidth);
        return nWidth;
    }

    public interface ColumnEventListener {
        void onColumnSizeChanged(int nColumnIndex, int nWidth);

        void onColumnSizeChangedFinished(int nColumnIndex, int nWidth);

        void onSortByChanged(int type);
    }
}
